package com.attendcollatesys.controller;

import com.attendcollatesys.entity.Employee;
import com.attendcollatesys.service.EmployeeService;
import com.attendcollatesys.web.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    EmployeeService employeeService;


    /**
     * 员工登录
     * @param request
     * @param employee
     * @return
     */
    @PostMapping("/login")
    //@RequestBody:封装json数据
    //登录成功后员工的id存放在session中，通过request对象可以获取session,进一步获取用户信息
    public ResponseResult<?> login(HttpServletRequest request, @RequestBody Employee employee){

        Map<String,String> empH = employeeService.LoginVerificationMethod(employee.getAccount(), employee.getPassword());

        //5、查看员工状态，如果为已禁用状态，则返回员工已禁用结果
        if(!"0".equals(empH.get("status"))){
            return new ResponseResult(403,"账户暂时无登录权限！");
        }else {
            //6、登录成功，将员工id存入Session并返回登录成功结果
            request.getSession().setAttribute("account:"+empH.get("account"),"1");

            request.getSession().setAttribute(empH.get("account")+":permission",empH.get("permission"));


            return new ResponseResult(200,"登录成功",empH);
            //account
        }
    }


    /**
     * 员工退出
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public ResponseResult<?> logout(HttpServletRequest request, String account ){
        //清理Session中保存的当前登录员工的id
        request.getSession().removeAttribute("account:"+account);
        request.getSession().removeAttribute(account+":permission");
        return new ResponseResult(200,"退出成功!");
    }

}
